#version 330 core
out vec4 FragColor;  // 输出片段颜色

in vec3 Normal;
in vec3 FragPos;
uniform sampler2D ourTexture;
uniform float alphaThreshold = 0.3;
uniform vec2 cameraPos;

vec3 calcDiffuse(vec3 normal, vec3 lightDir, vec3 lightColor){
    float diff = max(dot(normal, lightDir), 0.0);
    return lightColor * diff;
}

//vec3 calcFogColor(vec3 fragPos){
//    fragPos = normalize(fragPos);
//    vec3 skyColor = vec3(0.39608, 0.60392, 0.69020);
//    float miePhase = 1.0 / (4.0 * 3.14159);  // 简化Mie散射相位函数
//    float rayleighPhase = 3.0 / (16.0 * 3.14159) * (1.0 + fragPos.y * fragPos.y);  // Rayleigh散射
//    float scattering = rayleighPhase * 0.8 + miePhase * 0.2;  // 混合散射
//
//    // 根据高度角调整散射强度
//    float heightFactor = pow(1.0 - abs(fragPos.y), 4.0);  // 靠近地平线时散射更强
//    vec3 scatteredColor = skyColor * scattering * heightFactor * 7.5;
//
//    vec3 envColor = vec3(0.52941, 0.80784, 0.92157);
//    vec3 finalColor = envColor + scatteredColor;
//    return finalColor;
//}

void main()
{
    vec3 cloudColor = vec3(0.75, 0.75, 0.75);

    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(vec3(0.25, 0.5, 1.0));

    float intensity = 1.0;// 光强
    vec3 lightColor = vec3(1.0 * intensity, 0.843137 * intensity, 0.0);// 金黄色
    vec3 diffuse = calcDiffuse(norm, lightDir, lightColor);



    float saturation = 2.0;// 饱和度
    float luminance = dot(cloudColor, vec3(0.299, 0.587, 0.114));
    vec3 desaturated = mix(vec3(luminance), cloudColor, saturation);

    vec4 color1 = vec4(desaturated + diffuse, 0.8);

    float fogStart = 100.0;
    float fogEnd = 500.0;
    //vec4 fogColor = vec4(calcFogColor(vec3(FragPos.x - cameraPos.x, 0, FragPos.z - cameraPos.y)), 1.0);
    // 计算雾效因子
    float fogFactor = (fogEnd - length(vec3(FragPos.x - cameraPos.x, 0, FragPos.z - cameraPos.y))) / (fogEnd - fogStart);
    fogFactor = clamp(fogFactor, 0.0, 1.0);

    vec4 finalColor = color1;

    //finalColor = mix(finalColor, fogColor, 0.0);

    FragColor = vec4(finalColor.rgb, finalColor.a * fogFactor);
}